class MembersController < ApplicationController
  skip_before_filter :verify_authenticity_token

  #caches_page :list, :show
  #cache_sweeper :members_sweeper, :only => [:save, :destroy]

  def list

    if (params[:sort_by] != nil)
      if (params[:filter] != nil and params[:filter][:type] != "")
        filter = params[:filter][:type]
        if filter == "Alphas" or filter == "Phis" or filter == "Omegas"
          filter == "Alphas" ? filter = 0 : filter == "Phis" ? filter = 1 : filter == "Omegas" ? filter = 2 : ""
          @members = Member.find(:all, :conditions => {:branch => filter, :deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
        elsif filter == "All"
          @members = Member.find(:all, :conditions => {:deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
        else
          @members = Member.find(:all, :conditions => {:status => filter, :deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
        end
      else
        @members = Member.find(:all, :conditions => "deleted = false AND (status = 'Active' OR status = 'Pledge' OR status = 'New Active' OR status = 'Neophyte' OR status = 'Probate' OR status = 'Associate')", :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
      end
    else
      if (params[:filter] != nil and params[:filter][:type] != "")
        filter = params[:filter][:type]
        if filter == "Alphas" or filter == "Phis" or filter == "Omegas"
          filter == "Alphas" ? filter = 0 : filter == "Phis" ? filter = 1 : filter == "Omegas" ? filter = 2 : ""
          @members = Member.find(:all, :conditions => {:branch => filter, :deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
        elsif filter == "All"
          @members = Member.find(:all, :conditions => {:deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
        else
          @members = Member.find(:all, :conditions => {:status => filter, :deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
        end
      else
        @members = Member.find(:all, :conditions => "deleted = false AND (status = 'Active' OR status = 'Pledge' OR status = 'New Active' OR status = 'Neophyte' OR status = 'Probate' OR status = 'Associate')", :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
      end
    end

    @member = session[:user]
  end

  def list_src

    if (params[:sort_by] != nil)
      if (params[:filter] != nil)
        @members = Member.find(:all, :select => "first_name, last_name, signed_src, id", :conditions => {:status => params[:filter], :deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
      else
        @members = Member.find(:all, :select => "first_name, last_name, signed_src, id", :conditions => {:deleted => false}, :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
      end
    else
      if (params[:filter] != nil)
        @members = Member.find(:all, :select => "first_name, last_name, signed_src, id", :conditions => {:status => params[:filter], :deleted => false}, :order => "last_name, first_name ASC")
      else
        @members = Member.find(:all, :select => "first_name, last_name, signed_src, id", :conditions => {:deleted => false}, :order => "last_name, first_name ASC")
      end
    end

    @member = session[:user]

  end

  def quorum
    @members = Member.find(:all, :conditions => "(status = 'Active' OR status = 'Neophyte' OR status = 'Pledge') AND deleted = 0", :order => "last_name, first_name ASC") #, :order => "#{params[:sort_by]} ASC")
    @member = session[:user]
  end

  def officers
    if params[:semester].nil?
      @members = Position.find_all_by_semester_index(Event.getCurrentSemester, :order => "position")
    else
      @members = Position.find_all_by_semester_index(params[:semester][:index], :order => "position")
    end
    @semesters = (0..Event.getCurrentSemester).collect {|year| [Event.getFormattedSemesterByIndex(year), year]}.sort{|a,b| b[1] <=> a[1]}
  end

  def committees
    @committees = Event.find_all_by_semester_index_and_event_type_and_deleted(Event.getCurrentSemester, "Committee", false)
  end

  def create
    @member = Member.new
    render :action => "edit"
  end

  def show
    @member = Member.find(params[:id])
    @memberData = Member.getData(@member, Event.getCurrentSemester)
    @events = @member.events.find_all_by_semester_index_and_deleted(Event.getCurrentSemester, false, :order => "date DESC")
    @committee = Member.get_committee(@member, Event.getCurrentSemester)
  end

  def refresh
    @member = Member.find(params[:id])
    @member.up_to_date = false
    @member.save
    @memberData = Member.getData(@member, Event.getCurrentSemester)
    @events = @member.events.find_all_by_semester_index_and_deleted(Event.getCurrentSemester, false, :order => "date DESC")
    render :action => "show"
  end

  def deleted
    @members = Member.find(:all, :conditions => {:deleted => true}, :order => "last_name, first_name ASC")
  end

  def undelete
    @member = Member.find(:all,:conditions => {:id => "#{params[:member_id]}"})

    @member[0].deleted = false
    @member[0].save

    redirect_to :action => "deleted"
  end

  def submit_changed_password

    if params[:commit] == "Submit"
      @member = Member.find(params[:id])
      if !session[:user].admin
        @result = Member.authenticate(@member.username, params[:old_password])
      end
      if session[:user].admin || @result == @member
        if params[:new_password_one] == params[:new_password_two]
         @member.password = Member.hash_password(params[:new_password_one])
          if @member.save
            redirect_to :action => "show", :id => @member
          else
            render :action => "change_password", :id =>@member
          end
        else
          render :action => "change_password", :id =>@member
        end
      else
        render :action => "change_password", :id =>@member
      end
    else
      render :action => "change_password", :id =>@member
    end
  end

  def change_password
    @member = Member.find(params[:id])
  end

  def save
    if params[:id].nil?
      @member = Member.new
    else
      @member = Member.find(params[:id])
    end

    if params[:commit] == "Save"

      @member.attributes = params[:member]
      @member.up_to_date = false

      error = false

      if ((currentMember = Member.find(:all, :conditions =>{:username => "#{@member.username}"})[0]) != nil)
        if (currentMember.id != @member.id)
          error = true
        end
      end

      if @member.password == nil
          @member.password = Digest::SHA1.hexdigest(@member.last_name)
      end

      if error == false
        if @member.save
          redirect_to :action => "show", :id => @member.id
        else
          render :action => "edit"
        end
      else
         @error = "Member already exists"
        render :action => "edit"
      end

    elsif params[:commit] == "Remove"
      remove
    end

  end

  def remove
    @member = Member.find(params[:id])
    @member.deleted = true
    @member.save
    redirect_to :action => "list"
  end

  def edit
    @member = Member.find(params[:id])
  end

  def complete
    safeQuery = params[:member][:full_name].gsub("'", "\\\\'");
    @members = Member.find(:all, :conditions => "last_name LIKE '#{safeQuery}%' OR first_name LIKE '#{safeQuery}%'")
    render :partial => "complete"
  end

  def src
    @member = Member.find(session[:user])
  end

  def signed

    @member = Member.find(session[:user])
    if(params[:accept][:signed].to_s == "1")
      @member.signed_src = true
      @member.save
      render :inline => "<body bgcolor=#ffd700>Thanks for signing!</body>"
    else
      @error = "Unfortunately, you can't party officially without agreeing to this. =("
      render :action => "src"
    end

  end

  def admin

    if params[:commit] == "Reset SRC"
      Member.find(:all).each do |member|
        member.signed_src = false
        member.save
      end
      flash[:notice] = "All members reset!"
    end
    if params[:commit] == "Toggle SRC"
      src = Toggle.find_by_name("SRC")
      src.enabled = !src.enabled
      src.save
    end
    if params[:commit] == "Create"
      pledgeclass = PledgeClass.new
      pledgeclass.name = params[:admin][:pledgeclass]
      pledgeclass.save

      flash[:notice] = "Pledge class: #{params[:admin][:pledgeclass]} created!"
    end
    if params[:commit] == "Refresh members"
      Member.find(:all).each do |member|
        member.up_to_date = false
        member.save
      end
      redirect_to :action => "list"
    end
    if params[:commit] == "Randomly assign people to a branch"
      index = 0
      Member.find(:all).each do |member|
        if (member.status == "Active" or member.status == "Pledge" or member.status == "New Active" or member.status == "Neophyte" or member.status == "Probate" or member.status == "Associate") and member.deleted == false
          member.branch = index%3
          index = index + 1
        else
          member.branch = nil
        end
        member.save
      end
      flash[:notice] = "Branches assigned!"
    end

    @srcToggle = Toggle.find_by_name("SRC").enabled
    @admin = Member.find_all_by_admin(true)
    if session[:user].admin?
      @officers = Position.find_all_by_position_and_semester_index(0..299, Event.getCurrentSemester)
    end

  end

  def career
    @members = Member.find_all_by_status_and_deleted("Active", false)
    @hours = Member.get_career_hours(@members)
  end

  def year
    @members = Member.find_all_by_deleted(false)
    @hours = Member.get_year_hours(@members)
  end

end
